$(function () {
    processChart();
});

function processChart() {
    var url = window.location.href;
    var argsIndex = url.split("?skuid=");
    var skuid = argsIndex[1];
    var myChart = echarts.init(document.getElementById('productDetailInfo'));
    // 清空画布
    myChart.clear();

    $.ajax({
        type: "GET",
        dataType: "json",
        url: '/product/get-product-detail-info-package?skuid=' + skuid,
        success: function (result) {
            if (result.code === "0") {
                var productName = result.data.name;
                var minNormalPrice = result.data.minNormalPrice;
                var minPlusPrice = result.data.minPlusPrice;
                $('#jd-url-ontent').attr('href', result.data.clickUrl);
                var data = result.data.productDetailInfoVos;
                var dataLength = data.length;
                // alert(dataLength)
                var x_arr = [];
                var y_arr_np = [];
                var y_arr_pp = [];
                var y_arr_fnp = [];
                var y_arr_fpp = [];
                for (var i = 0; i < dataLength; i++) {
                    var createTime = data[i].createTime;
                    var tag = '';
                    var promotionDes = data[i].promotionDes;
                    if (promotionDes.length > 0) {
                        promotionDes = "\n促销:" + promotionDes;
                        tag = " | 促销 |"
                    }
                    var couponDes = data[i].couponDes;
                    if (couponDes.length > 0) {
                        couponDes = "\n优惠券:" + couponDes;
                        tag = tag + " | 优惠券 |"
                    }
                    var plusDes = data[i].plusDes;
                    if (plusDes.length > 0) {
                        plusDes = "\nplus专属优惠:" + plusDes;
                        tag = tag + " | plus95折 |"
                    }
                    var buyCount = "\n买" + data[i].finalNormalPriceCount + "件  (plus买" + data[i].finalPlusPriceCount + "件)";
                    x_arr.push(createTime + promotionDes + couponDes + plusDes + buyCount);
                    // x_arr.push(data[i].createTime + ";促销:" + data[i].promotionDes + ";优惠券:" + data[i].couponDes);
                    y_arr_np.push(data[i].normalPrice);
                    y_arr_pp.push(data[i].plusPrice);
                    y_arr_fnp.push(data[i].finalNormalPrice);
                    y_arr_fpp.push(data[i].finalPlusPrice);
                }
                // var dataZoomEnd = 9 * 100 / dataLength;
                // 显示标题，图例和空的坐标轴
                myChart.setOption({
                    title: {
                        text: productName,
                        subtext: '历史最低价[仅供参考]: ' + minNormalPrice + '元  (plus:' + minPlusPrice + '元)' +
                            '\n\n当前优惠组合[仅供参考]: ' + tag,
                        // 主副标题之间的间距
                        itemGap: 12,
                        top: 10,
                        textStyle: {
                            fontSize: 14
                        },
                        subtextStyle: {
                            fontSize: 14,
                            color: '#FF5151'
                        }
                    },
                    tooltip: {
                        trigger: 'axis',
                        // axisPointer: {
                        //     type: 'cross'
                        // },
                        confine: true,
                        extraCssText: 'white-space:pre-wrap'
                    },
                    // legend: {
                    //     data: ['', '', '', '', '正常价格', '最终正常价格', '', 'plus价格', '最终plus价格'],
                    //     right: 10
                    // },
                    grid: {
                        height: '60%',
                        // 直角坐标系内绘图网格
                        top: '26%',
                        right: '6%',
                        // bottom: '10%',
                        left: '14%'
                    },
                    xAxis: {
                        // name: '时间',
                        boundaryGap: false,
                        axisLabel: {
                            show: false,
                            // triggerEvent:false,
                            // rotate: 45,
                            // align: 'left',
                            // color: 'red',
                            // padding: [20, 0, 0, 0],
                            // fontFamily: 'Microsoft YaHei',
                            interval: 0
                        },
                        data: x_arr
                    },
                    yAxis: {
                        name: '价格(元)'
                        // nameLocation: 'middle',
                        // nameGap: 25
                        // nameRotate: 90
                    },
                    series: [{
                        name: '正常价格',
                        type: 'line',
                        data: y_arr_np
                    }, {
                        name: 'plus价格',
                        type: 'line',
                        data: y_arr_pp
                    }, {
                        name: '最终正常价格',
                        type: 'line',
                        data: y_arr_fnp
                    }, {
                        name: '最终plus价格',
                        type: 'line',
                        data: y_arr_fpp
                    }]
                    // dataZoom: [
                    //     {
                    //         id: 'dataZoomX',
                    //         type: 'slider',
                    //         start: 100 - dataZoomEnd,
                    //         end: 100,
                    //         // xAxisIndex: [0],
                    //         height: 20,
                    //         y: '15%'
                    //     },
                    //     {
                    //         type: 'inside',
                    //         show: true
                    //     }
                    // ]
                });

                processWatchContent(skuid);
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        }
    });
}

$(document).on("click", "#notice-price-content", function () {
    var url = window.location.href;
    var argsIndex = url.split("?skuid=");
    var skuid = argsIndex[1];
    $.prompt({
        title: '设置通知价格',
        // text: '设置通知价格',
        input: '',
        empty: false, // 是否允许为空
        onOK: function (input) {
            var jsonData = {"skuid": skuid, "noticePrice": input};
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/product/set-notice-price",
                data: JSON.stringify(jsonData),
                success: function (result) {
                    if (result.code === "0") {
                        processWatchContent(skuid);
                        $.toast.prototype.defaults.duration = 1000;//1秒
                        $.toast("操作成功", 'text');
                    } else if (result.code === "101001") {
                        // window.location.href = "/page/login";
                        $.toast.prototype.defaults.duration = 1000;//1秒
                        $.toast(result.message, 'text');
                    } else {
                        $.toast.prototype.defaults.duration = 1000;//1秒
                        $.toast(result.message, 'text');
                    }
                },
                error: function () {
                    $.toast.prototype.defaults.duration = 1000;//1秒
                    $.toast("无网络", 'text');
                }
            });
        },
        onCancel: function () {
            //点击取消
        }
    });
});

function processWatchContent(skuid) {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: '/product/check-watch?skuid=' + skuid,
        success: function (result) {
            if (result.code === "0") {
                var watch = result.data.watch;
                if (watch) {
                    document.getElementById("notice-price").style.display = "block";
                    var noticePrice = result.data.noticePrice;
                    $("#notice-price-content").text("通知价格:" + noticePrice + "元 (点击重新设置)");
                }
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        }
    });
}